#include "ClientTest.h"

void ClientTest::run_test(){
    try{
        qDebug() << "start to test" << Qt::endl;
        // test_sql();
        // test_helper();
        // test_network();
        // test_task_progress();
        // test_scan_progress();
        qDebug() << "end test" << Qt::endl;
    }catch(const std::exception &e){
        qCritical() << "Test found exception: " << e.what();
    }
}

/*      sql       */
void ClientTest::test_sql(){
    AnalysisSql* sql = AnalysisSql::instance();

    // insert test
    SqlDomain::AnalysisDomain createRes1 = sql->createPatient("tacom", "123456", "aaa.jpg");
    SqlDomain::AnalysisDomain createRes2 = sql->createPatient("tacom", "123456", "aaa.jpg");
    SqlDomain::AnalysisDomain createRes3 = sql->createPatient("tacom", "123456", "bbb.jpg");

    qDebug() << "res1 id " << createRes1.id << Qt::endl
             << "res2 id " << createRes2.id << Qt::endl
             << "res3 id " << createRes3.id << Qt::endl;

    // update test
    bool updateRes1 = sql->updateTaskStatus(createRes3.id, "8848", TaskApi::DEFAULT_RUNNING_OPTIONS, 0, "");
    bool updateRes2 = sql->updateTaskStatus(createRes1.id, "8848", TaskApi::DEFAULT_RUNNING_OPTIONS, 1, "failed");

    qDebug() << "res1 update " << updateRes1 << Qt::endl
             << "res2 update " << updateRes2 << Qt::endl;

    // query list
    QVector<SqlDomain::AnalysisDomain> queryList = sql->queryDataList();
    for(SqlDomain::AnalysisDomain d: queryList){
        qDebug() << "name: " << d.patientName << " id: " << d.testId << Qt::endl;
    }

    // deal list
    QVector<SqlDomain::AnalysisDomain> dealList = sql->queryDealList("tacom", "123456");
    for(SqlDomain::AnalysisDomain d: dealList){
        qDebug() << "id: " << d.id << Qt::endl
                 << "name: " << d.patientName << Qt::endl
                 << "testId: " << d.testId << Qt::endl
                 << "imageName: " << d.imageName << Qt::endl
                 << "taskid: " << d.taskId << Qt::endl
                 << "taskopt: " << d.taskOption << Qt::endl
                 << "status: " << d.taskStatus << Qt::endl
                 << "err: " << d.errMsg << Qt::endl;
    }


    return;
}

/*      通用函数   */
void ClientTest::test_helper(){
    QString fileName("/home/tacom/Development/Mounts/RemoteSync/Seafile/P-EM-DataTest/85-邵海燕-left-Anterior-1980.9-20230808152209.jpg");
    QString result = Helper::fixFileName(fileName);
    //QString result = Helper::_fixName("85-邵海燕-left-Anterior-1980.9-20230808152209.jpg");
    qDebug() << "out name" << result;
}

/*      网络  */
void ClientTest::test_network(){
    ClientTest::test_task_create();
    ClientTest::test_task_upload();
    ClientTest::test_task_put();
    QThread::sleep(3);  // 等3s
    ClientTest::test_task_query();
    ClientTest::test_result_query();
}

void ClientTest::test_task_create(){
    SyncTaskApi::RemoteApiResult res = SyncTaskApi::createRemoteTask(TaskApi::DEFAULT_USER_NAME);
    qDebug() << Qt::endl
             << "\t task create" << Qt::endl
             << "\t\t code" << res.code << Qt::endl
             << "\t\t result" << res.result << Qt::endl
             << "\t\t error"  << res.errMsg;
}

void ClientTest::test_task_upload(){
    /*
    curl -X 'POST' \
      'http://114.55.245.149:8051/gateway/task/post' \
      -H 'accept: application/json' \
      -H 'Content-Type: multipart/form-data' \
      -F 'files=@软著_9_.jpg;type=image/jpeg' \
      -F 'files=@软著_7_.jpg;type=image/jpeg' \
      -F 'query_key=8afcb08f7b34f7bbca248de26b7edb72cb77b2385c0eafc6a55d88c4807502f2'
    */
    SyncTaskApi::RemoteApiResult res = SyncTaskApi::uploadFileToTask(testId, testfileList);
    qDebug() << Qt::endl
             << "\t task upload" << Qt::endl
             << "\t\t code" << res.code << Qt::endl
             << "\t\t result" << res.result << Qt::endl
             << "\t\t error"  << res.errMsg;

}

void ClientTest::test_task_put(){
    SyncTaskApi::RemoteApiResult res = SyncTaskApi::putTask(testId, TaskApi::DEFAULT_RUNNING_OPTIONS);
    qDebug() << Qt::endl
             << "\t task put" << Qt::endl
             << "\t\t code" << res.code << Qt::endl
             << "\t\t result" << res.result << Qt::endl
             << "\t\t error"  << res.errMsg;
}

void ClientTest::test_task_query(){
    SyncTaskApi::ApiTaskProgress res = SyncTaskApi::taskQuery(testId);
    qDebug() << Qt::endl
             << "\t task query" << Qt::endl
             << "\t\t code" << res.code << Qt::endl
             << "\t\t result" << res.result << Qt::endl
             << "\t\t error"  << res.errMsg << Qt::endl
             << "\t\t task status"  << res.taskStatus << Qt::endl
             << "\t\t task msg" << res.taskMsg << Qt::endl
             << "\t\t task progress" << res.taskProgrss << Qt::endl;
}

void ClientTest::test_result_query(){
    QString file1 = QFileInfo(testfileList[0]).fileName();
    SyncTaskApi::RemoteApiResult res = SyncTaskApi::resultQuery(
        testId, file1, TaskApi::DEFAULT_RUNNING_OPTIONS
    );
    qDebug() << Qt::endl
             << "\t task result" << Qt::endl
             << "\t\t code" << res.code << Qt::endl
             << "\t\t result" << res.result << Qt::endl
             << "\t\t error"  << res.errMsg;
}

void ClientTest::test_task_progress(){
    const QString pN = "tacom";
    const QString pI = "123456";
    QVector<QString> f{
        "/home/tacom/Development/Mounts/RemoteSync/Seafile/P-EM-DataTest/20230718111947-test-right-center-1998.12-20230718153245-s.jpg"
    };
    const QString rO = TaskApi::DEFAULT_RUNNING_OPTIONS;

    qDebug()
        << "exec"  << execPath
        << "cache " << cachePath
        << "log" << logPath;

    AsyncRunner runner(pN, pI, f, rO);
    QObject::connect(&runner, &AsyncRunner::AsyncDone, [](const AsyncRunnerResult res){
        qDebug() << Qt::endl
                 << "\t runner result" << Qt::endl
                 << "\t\t status" << res.taskStatus << Qt::endl
                 << "\t\t taskId" << res.taskId << Qt::endl
                 << "\t\t taskOp"  << res.options << Qt::endl
                 << "\t\t ok" << res.okRes << Qt::endl
                 << "\t\t fail" << res.errMsg << Qt::endl;
    });
    runner.start();

    // TODO 另外说一句，Qt不按照指针创建线程的话，这个函数跑完，系统
    //      释放runner,会导致线程中断，实际使用采用全局变量什么的管理下
    //      这里比较随意
    runner.wait();
}


void ClientTest::test_scan_progress(){
    // init path
    ScanApiResult::RemoteScanApiResult res = SyncScanApi::initScanPath();
    qDebug() << "Remote scan Path: " << ScanRemoteApi::scanPath << Qt::endl;

    // Get Remote file
    ScanApiResult::RemoteScanApiCacheResult fileRes = SyncScanApi::getNextCache();

    qDebug() << "New Cache Get: " << Qt::endl
             << "fileName " << fileRes.fileName << Qt::endl
             << "id" << fileRes.id << Qt::endl;

    QThread::sleep(8);

    // consume
    ScanApiResult::RemoteScanApiResult consumeRes = SyncScanApi::consumeFileCache(fileRes.id);

    qDebug() << "Cache consume res: " << Qt::endl
             << "code " << consumeRes.code << Qt::endl
             << "msg " << consumeRes.errMsg << Qt::endl;
}

